package cs342.group1.messenger;

import java.util.ArrayList;

import com.example.grouponemessenger.R;
import com.example.grouponemessenger.R.layout;
import com.example.grouponemessenger.R.menu;

import cs342.group1.messenger.Conversation.GetAllMessagesForUser;
import cs342.group1.messenger.Conversation.SaveMessageText;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SavedMessages extends Activity {
	private int MyId;
	private Database db; 
	ArrayList<Message> savedMessages = null;
	
	private ListView savedMessagesListView;
	private ConversationAdapter savedAdapter;
	public static Context savedContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_saved_messages);
		savedContext = this;
		db = new Database(this);
		savedMessagesListView = (ListView) findViewById(R.id.savedMessages);
		savedMessagesListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
			{
				//Unsave message and remove from list
				Message clickedMessage = (Message) savedAdapter.getItem(position);
				Toast.makeText(savedContext, clickedMessage.toString(), Toast.LENGTH_LONG).show();
				UnsaveMessageText task4 = new UnsaveMessageText();
				task4.execute(clickedMessage.CloudId);
				boolean success = false;
				try {
					success = task4.get();
				}
				catch (Exception e) {
					success = false;
				}
				
				savedAdapter.remove(clickedMessage);
				savedMessages.remove(clickedMessage);
				
			}
			
		});
		db.open();
		int myId = db.CheckUserLoggedIn().CloudId;
		GetAllSavedMessages task = new GetAllSavedMessages();
		task.execute(myId);
		try {
			savedMessages = task.get();
		}
		catch (Exception e) {
			savedMessages = new ArrayList<Message>();
		}
		updateConversation(savedMessages);
	}
	
	public void updateConversation(ArrayList<Message> messages)
	{
		this.savedAdapter = new ConversationAdapter(this,R.layout.my_messages_list_item,messages);
		this.savedMessagesListView.setAdapter(savedAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.saved_messages, menu);
		return true;
	}
	
	public class GetAllSavedMessages extends AsyncTask<Integer, Void, ArrayList<Message>> {
		@Override
		protected ArrayList<Message> doInBackground(Integer... params) {
			db.open();
			return db.GetAllSavedMessages(params[0]);
		}
	}
	
	public class UnsaveMessageText extends AsyncTask<Integer, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			return new CloudHelper().UnsaveMessageText(params[0]);
		}
	}

}
